package com.webapp.dao;

import java.util.List;


import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.webapp.domain.entity.Area;

@Repository
public class AreaDAO extends AbstractDAO<Area> {

	
	/**
	 * 查询某级别的地区
	 * @param level
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Area> findAreaByLevel(String level){
		String hql = " from Area a where a.level = :level";
		
		
		return getSession().createQuery(hql)
				.setString("level", level)
				.list();
	}
	
	/**
	 * 查询某地区的下级地区
	 * @param id
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Area> findChils(Long id){
		String hql = " from Area a where a.parent.id = :id";
		return getSession().createQuery(hql)
				.setLong("id", id)
				.list();
	}
	
	/**
	 * 通过名称和级别获取一个地区信息
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Area getAreabyNameAndLevel(String name, String level){
		String hql = " from Area a where a.level = :level and a.name = :name";
		Query query = getSession().createQuery(hql)
			.setString("level", level)
			.setString("name", name);
		List<Area> list = query.list();
		return list.size() > 0?list.get(0):null;

	}
}
